/**
 * problem_010.c
 * Copyright (C) 2011-03-18 - xrose
 * Find the sum of all the primes below two million.
 * result: 142913828922
 */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <gmp.h>
#define marray 2000000
int isPrime(int numb);
int main (int argc, char *argv[])
{
    int i;
    long long int j = 0;
    mpz_t sumprime;
    mpz_t tmp;
    int sol = floor(sqrt(marray));
    char snumb[marray];

    for(j=0; j<marray; j++)
    {
        snumb[j]=0;
    }
    for(i=2; i<=sol; i++)
    {
        if(isPrime(i))
        {
            for(j=i+i; j < (marray); j+=i)
            {
                snumb[j]=1;
            }
        }
    }

mpz_init(sumprime); //init bigInt
mpz_init(tmp);
mpz_set_d(sumprime, 0);

    for(j=2; j<marray; j++)
    {
        if(snumb[j]==0)
        {
            mpz_set_d(tmp, j);
            mpz_add(sumprime, sumprime, tmp);
        }
    }
    printf("Answer is:");
    mpz_out_str(stdout,10,sumprime);
    return 0;
}

/*is a prime?*/
int isPrime(int numb)
{
    int sqrtnumb = floor(sqrt(numb));
    int i;
    if(numb < 2)
    {
        return 0;
    } else
        if(numb == 2) return 1;
    else
        if(numb%2 == 0) return 0;
    for(i = 3; i<=sqrtnumb; i=i+2)
    {
        if(numb%i == 0)
        {
            return 0;
        }
    }
    return 1;
}
